/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.business.rest;

import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import lombok.extern.slf4j.Slf4j;
import me.zhengjie.annotation.Log;
import me.zhengjie.business.domain.Hljg;
import me.zhengjie.business.domain.Qy;
import me.zhengjie.business.domain.Zhsq;
import me.zhengjie.business.domain.sup.Gsgl;
import me.zhengjie.business.domain.vo.QyHomeJyVO;
import me.zhengjie.business.service.GsglService;
import me.zhengjie.business.service.HljgService;
import me.zhengjie.business.service.QyService;
import me.zhengjie.business.service.dto.QyQueryCriteria;
import me.zhengjie.utils.SecurityUtils;
import org.springframework.data.domain.Pageable;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.*;
import java.io.IOException;
import java.text.ParseException;
import java.util.*;
import javax.servlet.http.HttpServletResponse;

/**
* @website https://el-admin.vip
* @author xiahongbo
* @date 2022-07-14
**/
@Slf4j
@RestController
@RequiredArgsConstructor
@Api(tags = "签约管理")
@RequestMapping("/api/qy")
public class QyController {

    private final QyService qyService;
    private final HljgService hljgService;

    private final GsglService gsglService;

    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
    @PreAuthorize("@el.check('qy:list')")
    public void exportQy(HttpServletResponse response, QyQueryCriteria criteria) throws IOException {
        qyService.download(qyService.queryAll(criteria), response);
    }

    @GetMapping
    @ApiOperation("查询签约")
    @PreAuthorize("@el.check('qy:list')")
    public ResponseEntity<Object> queryQy(QyQueryCriteria criteria, Pageable pageable){
        return new ResponseEntity<>(qyService.queryAll(criteria,pageable),HttpStatus.OK);
    }

    @GetMapping(value = "/queryAllQyList")
    @ApiOperation("查询所有签约")
    public ResponseEntity<Object> queryAllQyList(QyQueryCriteria criteria, Pageable pageable) {
        return new ResponseEntity<>(qyService.queryAllQyList("", criteria.getBisQyZt(), criteria.getBlurry(), pageable),HttpStatus.OK);
    }

    @GetMapping(value = "/queryDqyList")
    @ApiOperation("查询待签约")
    public ResponseEntity<Object> queryDqyList(QyQueryCriteria criteria, Pageable pageable) {
        String hljgId = gsglService.queryGsgl(SecurityUtils.getCurrentUserId()).getBisGsglId();
        String zt = "qygl01";
    //    return new ResponseEntity<>(qyService.queryList(hljgId, zt, criteria.getBlurry(), pageable),HttpStatus.OK);
        return new ResponseEntity<>(qyService.queryList(hljgId, zt, criteria.getBlurry(), criteria.getBisZhsqHldzSsqx(), pageable),HttpStatus.OK);
    }

    @GetMapping(value = "/queryYfpList")
    @ApiOperation("查询已分配")
    public ResponseEntity<Object> queryYfpList(QyQueryCriteria criteria, Pageable pageable) {
        String hljgId = gsglService.queryGsgl(SecurityUtils.getCurrentUserId()).getBisGsglId();
        String zt = "qygl02";
    //    return new ResponseEntity<>(qyService.queryList(hljgId, zt, criteria.getBlurry(), pageable),HttpStatus.OK);
        return new ResponseEntity<>(qyService.queryList(hljgId, zt, criteria.getBlurry(), criteria.getBisZhsqHldzSsqx(), pageable),HttpStatus.OK);
    }

    @GetMapping(value = "/queryYqyList")
    @ApiOperation("查询已签约")
    public ResponseEntity<Object> queryYqyList(QyQueryCriteria criteria, Pageable pageable) {
        String hljgId = gsglService.queryGsgl(SecurityUtils.getCurrentUserId()).getBisGsglId();
        String zt = "qygl03";
        return new ResponseEntity<>(qyService.queryList(hljgId, zt, criteria.getBlurry(), criteria.getBisZhsqHldzSsqx(), pageable),HttpStatus.OK);
    }

    @PostMapping
    @Log("新增签约")
    @ApiOperation("新增签约")
    @PreAuthorize("@el.check('qy:add')")
    public ResponseEntity<Object> createQy(@Validated @RequestBody Qy resources){
        return new ResponseEntity<>(qyService.create(resources),HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改签约")
    @ApiOperation("修改签约")
    @PreAuthorize("@el.check('qy:edit')")
    public ResponseEntity<Object> updateQy(@Validated @RequestBody Qy resources){
        qyService.update(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @PutMapping(value = "/updateQyzfzt")
    @Log("取消预签约")
    @ApiOperation("取消预签约")
    public ResponseEntity<Object> updateQyzfzt(@RequestParam String qyid){
        qyService.updateQyzfzt(qyid);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @DeleteMapping
    @Log("删除签约")
    @ApiOperation("删除签约")
    @PreAuthorize("@el.check('qy:del')")
    public ResponseEntity<Object> deleteQy(@RequestBody String[] ids) {
        qyService.deleteAll(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    @GetMapping(value = "/qyHomeJY")
    @Log("居家解约列表")
    @ApiOperation("居家解约列表")
    public ResponseEntity<Object> qyHomeJY(QyQueryCriteria qyHomeJyVO, Pageable pageable){


        Long userid = SecurityUtils.getCurrentUserId();
        Gsgl gsgl = gsglService.queryGsgl(userid);
       /* Hljg hljg = new Hljg();
        hljg.setBisGsglId(gsgl.getBisGsglId());*/

        List<String> list = new ArrayList<>();
        list.add("qygl01");
        list.add("qygl02");
        list.add("qygl03");

        qyHomeJyVO.setQyzt(list);


        UserDetails userDetails = SecurityUtils.getCurrentUser();

        JSONObject userJson = new JSONObject(new JSONObject(userDetails).get("user"));
        JSONArray array = JSONUtil.parseArray(userJson.get("roles"));
        String roleString = array.toString();
        log.info("queryKc21 roleString:{}", roleString);
        if(roleString.indexOf("居家照护") != -1 || roleString.indexOf("居家机构双角色") != -1) {
            log.info("角色：居家照护、机构照护、居家机构双角色");
            qyHomeJyVO.setHljg(gsgl.getBisGsglId());
        }


        return new ResponseEntity<>(qyService.qyHomeJY(qyHomeJyVO,pageable), HttpStatus.OK);
    }

    @PutMapping(value = "/qyHomeJYUpdate")
    @Log("居家解约更改")
    @ApiOperation("居家解约更改")
    public ResponseEntity<Object> qyHomeJYUpdate(@RequestBody HashMap<String, String> map) throws ParseException {
        qyService.qyHomeJYUpdate(map);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @GetMapping(value = "/qyHomeYjy")
    @Log("居家已解约列表")
    @ApiOperation("居家已解约列表")
    public ResponseEntity<Object> qyHomeYjy(QyQueryCriteria qyHomeJyVO, Pageable pageable){
        List<String> list = new ArrayList<>();
        list.add("qygl04");
        qyHomeJyVO.setQyzt(list);
        return new ResponseEntity<>(qyService.qyHomeYjy(qyHomeJyVO,pageable), HttpStatus.OK);
    }

    @GetMapping(value = "/qyHomeCxjg")
    @Log("重新选择机构")
    @ApiOperation("重新选择机构")
    public ResponseEntity<Object> qyHomeCxjg(QyQueryCriteria qyHomeJyVO, Pageable pageable){

        Long userid = SecurityUtils.getCurrentUserId();
        Gsgl gsgl = gsglService.queryGsgl(userid);
        Hljg hljg = new Hljg();
        if(gsgl != null){
            hljg.setBisGsglId(gsgl.getBisGsglId());
        } else {
            hljg.setBisGsglId("");
        }
        List<String> list = new ArrayList<>();
        list.add("qygl01");
        list.add("qygl02");
        list.add("qygl03");

        qyHomeJyVO.setQyzt(list);
        qyHomeJyVO.setHljg(hljg.getBisGsglId());
        return new ResponseEntity<>(qyService.qyHomeJY(qyHomeJyVO,pageable), HttpStatus.OK);
    }

    @PutMapping(value = "/qyHomeCxjgUpdate")
    @Log("提交重选机构")
    @ApiOperation("提交重选机构")
    public ResponseEntity<Object> qyHomeCxjgUpdate(@RequestBody HashMap<String, String> qyHomeJyVO){
        qyService.qyHomeCxjgUpdate(qyHomeJyVO);
        return new ResponseEntity<>( HttpStatus.OK);
    }
}